// import 'package:app_interface/app_interface.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

// import '../../app_doorbell_setup_noti_logic.dart';

class EmailDialogController extends GetxController {
  final email = ''.obs;
  final emailError = RxString('');
  final isOk = false.obs;
  late TextEditingController emailTextController;
  // final notiLogic = Get.find<AppDoorbellSetupNotiLogic>();
  @override
  void onInit() {
    emailTextController = TextEditingController(text: email.value);
    super.onInit();
  }

  void validateEmail(String value) {
    email.value = value;
    if (value.isEmpty) {
      emailError.value = '邮箱格式不对!'.tr;
    } else if (!RegExp(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")
        .hasMatch(value)) {
      emailError.value = '邮箱格式不对!'.tr;
    } else {
      // var mailList = notiLogic.state?.Relation_mailList;
      // if (mailList != null && mailList.contains(value)) {
      //   emailError.value = '邮箱已存在!'.tr;
      //   return;
      // }
      isOk.value = true;
      emailError.value = '';
    }
  }

  // 添加清除邮箱的方法
  void clearEmail() {
    emailTextController.clear();
    isOk.value = false;
    email.value = '';
    emailError.value = '';
  }

  /// 点击确定
  void submit() async {
    if (isOk.value) {
      Get.back(result: email.value);
    }
  }

  @override
  void onClose() {
    clearEmail();
    emailTextController.dispose();
    super.onClose();
  }
}
